package com.star.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.star.domain.Order;
import com.star.domain.Shop;
import com.star.domain.ShopType;
import com.star.domain.UserLogin;
import com.star.dto.FlowloadUtils;
import com.star.dto.ResponseData;
import com.star.dto.Result;
import com.star.mapper.RoleMapper;
import com.star.mapper.ShopMapper;
import com.star.service.ShopService;
import com.star.service.UserLoginService;
import com.star.util.InspectionTokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.nio.FloatBuffer;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Star
 * @since 2021-04-21
 */
@Controller
@RequestMapping("/shop")
public class ShopController {
    //店铺表
    @Autowired
    private ShopService shopService;
    //查询
    private UserLoginService userLoginService;

    /**
     * 注册店铺
     *
     * @param shop
     * @return
     */
    @RequestMapping("/registerShop")
    private @ResponseBody
    ResponseData registerShop(Shop shop, UserLogin userLogin, String areaName, ShopType shopType) {
        ResponseData responseData = null;
        boolean b = shopService.shopRegister(shop, userLogin, areaName, shopType);
        //控制判断 如果插入成功
        if (b) {
            responseData = ResponseData.ok();
            responseData.putDataValue("userLogin", userLoginService.LoginByLogId(userLogin));
        } else {
            responseData = ResponseData.customerError();
        }
        return responseData;
    }

    /**
     * 查询所有店铺 分页
     *总页数=（总数-1）/每页数量+1
     * 总页数=（总数+每页数量-1）/每页数量
     * @return
     */
    @RequestMapping("/findShopAll")
    @ResponseBody
    private HashMap<String, Object> findShopAll( @RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                                 @RequestParam(name = "limit", required = true, defaultValue = "10") int size) {
        IPage<Map<String, Object>> clientPage = shopService.findClientPage(page, size);

/*      System.out.println(clientPage.getTotal());//总数量
        System.out.println(clientPage.getCurrent());//当前页数
        System.out.println(clientPage.getSize());//每页数量
        System.out.println(clientPage.getPages());//总页数
*/

        return FlowloadUtils.buildResult(clientPage,clientPage.getRecords());
    }

    /**
     * 根据shopId查询 店铺信息
     * @param shopId
     * @return
     */
    @RequestMapping("/findShopById")
    @ResponseBody
    private ResponseData findShopId(Integer shopId){
        ResponseData responseData = null;
        Shop shop = shopService.shopById(shopId);
        if (shop==null){
            responseData = ResponseData.customerError();
        }else {
            responseData = ResponseData.ok();
            responseData.putDataValue("shop",shop);
        }
        return responseData;
    }



}

